package com.babyshop.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.babyshop.domain.Type;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class TypeDaoImpl implements TypeDao {

	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	@Override
	public void saveOfUpdate(Type type) {
		try {
			session.saveOrUpdate(type);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public void delete(int id) {
		try {
			Type type = (Type) session.get(Type.class, id);
			session.delete(type);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public Type get(int id) {
		Type type = null;
		try {
			type = (Type) session.get(Type.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return type;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Type> getAll() {
		List<Type> listTypes = null;
		try {
			listTypes = session.createQuery("from Type").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listTypes;
	}

}
